package control;

import java.util.ArrayList;

import boundary.BoardPanel;
import boundary.ChartPanel;


import entity.*;

public class PeciesController {
	
	private String grandFaName, grandMoName, moName, faName, sonName,daughterName;
	
	public void createPecies(String role,boolean isChinese){
		setNameLang(isChinese);
		if(role == "father"){
						Pecies p =new Father("/images/pieces/father.PNG","/images/pieces/icon/father_big.PNG", faName,10,80, 80);
                        p.setEventLog(new EventLog());
                }
		else if(role == "mother"){
						Pecies p = new Mother("/images/pieces/mom.PNG","/images/pieces/icon/mon_big.PNG", moName,10,80,80);
                        p.setEventLog(new EventLog());
                }
                else if(role == "son"){
			Pecies p = new Son("/images/pieces/son.PNG","/images/pieces/icon/son_big.PNG", sonName,10,80,80);
                        p.setEventLog(new EventLog());
                }
                else if(role == "daughter"){
			Pecies p =new Daughter("/images/pieces/sister.PNG","/images/pieces/icon/sister_big.PNG", daughterName,10,80,80);
                        p.setEventLog(new EventLog());
                }
                else if(role == "grandmother"){
			Pecies p =new Grandmother("/images/pieces/grandmon.PNG","/images/pieces/icon/grandmon_big.PNG",grandMoName,10,80,80);
                        p.setEventLog(new EventLog());
                }
                else{
			Pecies p =new Grandfather("/images/pieces/grandfather.PNG","/images/pieces/icon/grandfather_big.PNG",grandFaName,10,80,80);
                        p.setEventLog(new EventLog());
                }
        }
	public void setNameLang(boolean isChinese) {
		if (isChinese) {
			grandFaName = "GrandFa Chin";
			grandMoName = "GrandMom Chin";
			moName = "Mother Chin";
			faName = "Father Chin";
			sonName = "Son Chin";
			daughterName = "Daughter Chin";
		} else {
			grandFaName = "GrandFa";
			grandMoName = "GrandMom";
			moName = "Mother";
			faName = "Father";
			sonName = "Son";
			daughterName = "Daughter";
		}
	}
	
	public void playerMoveInBoard(BoardPanel bp, Pecies moveObj){

		if(moveObj.getMoveDirection()==0 && bp._x>-405 && bp._y>=0) bp._x-=5;//have changed _x for 800x800
		else if(moveObj.getMoveDirection()==0 && moveObj.getX() < 665) moveObj.move(5,0);
		else if (moveObj.getMoveDirection() == 0 && bp._x<=-405 && bp._y>-605) {
			moveObj.setMoveDirection(1);
		}
		
		/**movement: up to down*/
		if(moveObj.getMoveDirection()==1 && bp._y>-570 && bp._x<=-405) bp._y-=5;
		else if(moveObj.getMoveDirection()==1 && moveObj.getY() < 445) moveObj.move(0,5);
		else if(moveObj.getMoveDirection()==1 && bp._x<=-405) moveObj.setMoveDirection(2);
		
		/**movement: right to left*/
		if(moveObj.getMoveDirection()==2 && bp._x<0 && bp._y<=-405) bp._x+=5;
		else if(moveObj.getMoveDirection()==2 && moveObj.getX() > 40) moveObj.move(-5,0);
		else if(moveObj.getMoveDirection()==2 && bp._x>=0 && bp._y<=605) moveObj.setMoveDirection(3);
		
		/**movement: down to up*/
		if(moveObj.getMoveDirection()==3 && bp._y<0 && bp._x<=0) bp._y+=5;
		else if(moveObj.getMoveDirection()==3 && moveObj.getY() > 40) moveObj.move(0,-5);
		else if(moveObj.getMoveDirection()==3 && bp._y>=0) moveObj.setMoveDirection(0);
		
	}
	
	public void keepLocation(BoardPanel bp, Pecies moveObj,int storedX,int storedY){
		/**Keep other players location*/
		ArrayList<GameMovableObj> al = GameMovableObj.getGameMovableObj();
		
		for(int i=0; i<al.size(); i++) {
			if(al.get(i) != moveObj	) {
				Pecies c = (Pecies)al.get(i);
				c.move(bp._x-storedX,bp._y-storedY);
			}
		}
	}
	
	public ArrayList<Pecies> getAllPecies() {
		ArrayList<Pecies> ap = new ArrayList<Pecies>();
		ArrayList<GameMovableObj> temp = Pecies.getGameMovableObj();
		for(int i =0; i<temp.size(); i++) {
			if(temp.get(i) instanceof Pecies) {
				ap.add((Pecies)temp.get(i));
			}
		}
		return ap;
	}
	
	public void checkEndGame(Pecies moveObj){
		/**close all windows*/
		if(moveObj.getSelf() >= 20){
			System.gc();
			java.awt.Window win[]= java.awt.Window.getWindows();
			for(int i=0; i<win.length; i++){
				win[i].dispose();
				win[i] = null;
			}
			new ChartPanel();
		}
		
	}
	
}
